package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.Comment;
import com.zzc.fwzpxt.service.ICommentService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import com.zzc.common.utils.StringUtil;
import com.zzc.sys.entity.User;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  留言控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-09-26
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentController extends BaseController {
    @Autowired
    private ICommentService iCommentService;

    @GetMapping("/list")
    public String list(){
        return blogPath + "comment/list";
    }

    /**
     * 获取留言列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索内容
     * @return
     */
    @ResponseBody
    @RequestMapping("/getList")
    public Map<String,Object> getList(int pageNumber, int pageSize, String searchText){
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Comment> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Comment> entityWrapper = new EntityWrapper<>();
        if (StringUtil.isNotEmpty(searchText)) {
            entityWrapper.like("comment", searchText);
        }
        entityWrapper.where("status!=0");
        entityWrapper.orderBy("gmt_create", false);
        Page<Comment> commentPage = iCommentService.selectPage(page,entityWrapper);
        result.put("total", commentPage.getTotal());
        result.put("rows", commentPage.getRecords());
        return result;
    }
    /**
     * 添加留言
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addComment")
    public JsonResult addComment(Comment comment) {
        return  iCommentService.insert(comment) ? renderSuccess("添加成功") : renderError("添加失败");
    }
    /**
     * 修改留言
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateComment")
    public JsonResult updateComment(Comment comment) {
        return iCommentService.updateById(comment) ? renderSuccess("修改成功") : renderError("修改失败");
    }
    /**
     * 删除留言
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        return iCommentService.deleteCommentByIds(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }
    /**
     * 审核留言
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/audit")
    public JsonResult auditComment(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        return iCommentService.auditComment(id) ? renderSuccess("审核成功") : renderError("审核失败");
    }
}